package com.example.d10_homework.homework02;

/**
 * 线程抄写类
 */
public class CopyThread implements Runnable {
    private Punishment punishment;

    public CopyThread(Punishment punishment) {
        this.punishment = punishment;
    }

    @Override
    public void run() {
        int count = 0;
        String name = Thread.currentThread().getName();
        while (true) {
            synchronized (this) {
                if (punishment.getLeftCount() > 0) {
                    // 获取剩余次数
                    int leftCount = punishment.getLeftCount();
                    System.out.println(name + ":抄写了一遍" + punishment.getCopyWord() + ",还有" + --leftCount + "次");
                    // 修改剩余的次数
                    punishment.setLeftCount(leftCount);
                    count++;
                } else {
                    // 抄完了
                    break;
                }
            }
        }
        System.out.println(name + "抄写结束,抄写了" + count + "次");
    }
}
